package com.ledim.db.dao;

import java.util.List;
import java.util.ArrayList;
import android.database.Cursor;
import android.database.sqlite.SQLiteStatement;

import org.greenrobot.greendao.AbstractDao;
import org.greenrobot.greendao.Property;
import org.greenrobot.greendao.internal.SqlUtils;
import org.greenrobot.greendao.internal.DaoConfig;
import org.greenrobot.greendao.database.Database;
import org.greenrobot.greendao.database.DatabaseStatement;

import com.ledim.bean.LedimPicBean;

import com.ledim.bean.LedimChoiceCardBean;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/** 
 * DAO for table "CARD".
*/
public class LedimChoiceCardBeanDao extends AbstractDao<LedimChoiceCardBean, Long> {

    public static final String TABLENAME = "CARD";

    /**
     * Properties of entity LedimChoiceCardBean.<br/>
     * Can be used for QueryBuilder and for referencing column names.
    */
    public static class Properties {
        public final static Property Id = new Property(0, Long.class, "id", true, "Id");
        public final static Property PhotoId = new Property(1, long.class, "photoId", false, "photo");
        public final static Property Type = new Property(2, int.class, "type", false, "type");
        public final static Property Title = new Property(3, String.class, "title", false, "title");
        public final static Property Subtitle = new Property(4, String.class, "subtitle", false, "subtitle");
        public final static Property Media = new Property(5, String.class, "media", false, "media");
        public final static Property Submedia = new Property(6, String.class, "submedia", false, "submedia");
        public final static Property Link = new Property(7, String.class, "link", false, "link");
        public final static Property PlayedDuration = new Property(8, long.class, "playedDuration", false, "playedDuration");
        public final static Property PlayedEpisode = new Property(9, String.class, "playedEpisode", false, "playedEpisode");
        public final static Property Time = new Property(10, long.class, "time", false, "time");
        public final static Property NameCn = new Property(11, String.class, "nameCn", false, "nameCn");
    };

    private DaoSession daoSession;


    public LedimChoiceCardBeanDao(DaoConfig config) {
        super(config);
    }
    
    public LedimChoiceCardBeanDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
        this.daoSession = daoSession;
    }

    /** Creates the underlying database table. */
    public static void createTable(Database db, boolean ifNotExists) {
        String constraint = ifNotExists? "IF NOT EXISTS ": "";
        db.execSQL("CREATE TABLE " + constraint + "\"CARD\" (" + //
                "\"Id\" INTEGER PRIMARY KEY AUTOINCREMENT ," + // 0: id
                "\"photo\" INTEGER NOT NULL ," + // 1: photoId
                "\"type\" INTEGER NOT NULL ," + // 2: type
                "\"title\" TEXT," + // 3: title
                "\"subtitle\" TEXT," + // 4: subtitle
                "\"media\" TEXT," + // 5: media
                "\"submedia\" TEXT," + // 6: submedia
                "\"link\" TEXT," + // 7: link
                "\"playedDuration\" INTEGER NOT NULL ," + // 8: playedDuration
                "\"playedEpisode\" TEXT," + // 9: playedEpisode
                "\"time\" INTEGER NOT NULL ," + // 10: time
                "\"nameCn\" TEXT);"); // 11: nameCn
    }

    /** Drops the underlying database table. */
    public static void dropTable(Database db, boolean ifExists) {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"CARD\"";
        db.execSQL(sql);
    }

    @Override
    protected final void bindValues(DatabaseStatement stmt, LedimChoiceCardBean entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
        stmt.bindLong(2, entity.getPhotoId());
        stmt.bindLong(3, entity.getType());
 
        String title = entity.getTitle();
        if (title != null) {
            stmt.bindString(4, title);
        }
 
        String subtitle = entity.getSubtitle();
        if (subtitle != null) {
            stmt.bindString(5, subtitle);
        }
 
        String media = entity.getMedia();
        if (media != null) {
            stmt.bindString(6, media);
        }
 
        String submedia = entity.getSubmedia();
        if (submedia != null) {
            stmt.bindString(7, submedia);
        }
 
        String link = entity.getLink();
        if (link != null) {
            stmt.bindString(8, link);
        }
        stmt.bindLong(9, entity.getPlayedDuration());
 
        String playedEpisode = entity.getPlayedEpisode();
        if (playedEpisode != null) {
            stmt.bindString(10, playedEpisode);
        }
        stmt.bindLong(11, entity.getTime());
 
        String nameCn = entity.getNameCn();
        if (nameCn != null) {
            stmt.bindString(12, nameCn);
        }
    }

    @Override
    protected final void bindValues(SQLiteStatement stmt, LedimChoiceCardBean entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
        stmt.bindLong(2, entity.getPhotoId());
        stmt.bindLong(3, entity.getType());
 
        String title = entity.getTitle();
        if (title != null) {
            stmt.bindString(4, title);
        }
 
        String subtitle = entity.getSubtitle();
        if (subtitle != null) {
            stmt.bindString(5, subtitle);
        }
 
        String media = entity.getMedia();
        if (media != null) {
            stmt.bindString(6, media);
        }
 
        String submedia = entity.getSubmedia();
        if (submedia != null) {
            stmt.bindString(7, submedia);
        }
 
        String link = entity.getLink();
        if (link != null) {
            stmt.bindString(8, link);
        }
        stmt.bindLong(9, entity.getPlayedDuration());
 
        String playedEpisode = entity.getPlayedEpisode();
        if (playedEpisode != null) {
            stmt.bindString(10, playedEpisode);
        }
        stmt.bindLong(11, entity.getTime());
 
        String nameCn = entity.getNameCn();
        if (nameCn != null) {
            stmt.bindString(12, nameCn);
        }
    }

    @Override
    protected final void attachEntity(LedimChoiceCardBean entity) {
        super.attachEntity(entity);
        entity.__setDaoSession(daoSession);
    }

    @Override
    public Long readKey(Cursor cursor, int offset) {
        return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
    }    

    @Override
    public LedimChoiceCardBean readEntity(Cursor cursor, int offset) {
        LedimChoiceCardBean entity = new LedimChoiceCardBean( //
            cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id
            cursor.getLong(offset + 1), // photoId
            cursor.getInt(offset + 2), // type
            cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3), // title
            cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4), // subtitle
            cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5), // media
            cursor.isNull(offset + 6) ? null : cursor.getString(offset + 6), // submedia
            cursor.isNull(offset + 7) ? null : cursor.getString(offset + 7), // link
            cursor.getLong(offset + 8), // playedDuration
            cursor.isNull(offset + 9) ? null : cursor.getString(offset + 9), // playedEpisode
            cursor.getLong(offset + 10), // time
            cursor.isNull(offset + 11) ? null : cursor.getString(offset + 11) // nameCn
        );
        return entity;
    }
     
    @Override
    public void readEntity(Cursor cursor, LedimChoiceCardBean entity, int offset) {
        entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
        entity.setPhotoId(cursor.getLong(offset + 1));
        entity.setType(cursor.getInt(offset + 2));
        entity.setTitle(cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3));
        entity.setSubtitle(cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4));
        entity.setMedia(cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5));
        entity.setSubmedia(cursor.isNull(offset + 6) ? null : cursor.getString(offset + 6));
        entity.setLink(cursor.isNull(offset + 7) ? null : cursor.getString(offset + 7));
        entity.setPlayedDuration(cursor.getLong(offset + 8));
        entity.setPlayedEpisode(cursor.isNull(offset + 9) ? null : cursor.getString(offset + 9));
        entity.setTime(cursor.getLong(offset + 10));
        entity.setNameCn(cursor.isNull(offset + 11) ? null : cursor.getString(offset + 11));
     }
    
    @Override
    protected final Long updateKeyAfterInsert(LedimChoiceCardBean entity, long rowId) {
        entity.setId(rowId);
        return rowId;
    }
    
    @Override
    public Long getKey(LedimChoiceCardBean entity) {
        if(entity != null) {
            return entity.getId();
        } else {
            return null;
        }
    }

    @Override
    protected final boolean isEntityUpdateable() {
        return true;
    }
    
    private String selectDeep;

    protected String getSelectDeep() {
        if (selectDeep == null) {
            StringBuilder builder = new StringBuilder("SELECT ");
            SqlUtils.appendColumns(builder, "T", getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T0", daoSession.getLedimPicBeanDao().getAllColumns());
            builder.append(" FROM CARD T");
            builder.append(" LEFT JOIN PHOTO T0 ON T.\"photo\"=T0.\"Id\"");
            builder.append(' ');
            selectDeep = builder.toString();
        }
        return selectDeep;
    }
    
    protected LedimChoiceCardBean loadCurrentDeep(Cursor cursor, boolean lock) {
        LedimChoiceCardBean entity = loadCurrent(cursor, 0, lock);
        int offset = getAllColumns().length;

        LedimPicBean photo = loadCurrentOther(daoSession.getLedimPicBeanDao(), cursor, offset);
         if(photo != null) {
            entity.setPhoto(photo);
        }

        return entity;    
    }

    public LedimChoiceCardBean loadDeep(Long key) {
        assertSinglePk();
        if (key == null) {
            return null;
        }

        StringBuilder builder = new StringBuilder(getSelectDeep());
        builder.append("WHERE ");
        SqlUtils.appendColumnsEqValue(builder, "T", getPkColumns());
        String sql = builder.toString();
        
        String[] keyArray = new String[] { key.toString() };
        Cursor cursor = db.rawQuery(sql, keyArray);
        
        try {
            boolean available = cursor.moveToFirst();
            if (!available) {
                return null;
            } else if (!cursor.isLast()) {
                throw new IllegalStateException("Expected unique result, but count was " + cursor.getCount());
            }
            return loadCurrentDeep(cursor, true);
        } finally {
            cursor.close();
        }
    }
    
    /** Reads all available rows from the given cursor and returns a list of new ImageTO objects. */
    public List<LedimChoiceCardBean> loadAllDeepFromCursor(Cursor cursor) {
        int count = cursor.getCount();
        List<LedimChoiceCardBean> list = new ArrayList<LedimChoiceCardBean>(count);
        
        if (cursor.moveToFirst()) {
            if (identityScope != null) {
                identityScope.lock();
                identityScope.reserveRoom(count);
            }
            try {
                do {
                    list.add(loadCurrentDeep(cursor, false));
                } while (cursor.moveToNext());
            } finally {
                if (identityScope != null) {
                    identityScope.unlock();
                }
            }
        }
        return list;
    }
    
    protected List<LedimChoiceCardBean> loadDeepAllAndCloseCursor(Cursor cursor) {
        try {
            return loadAllDeepFromCursor(cursor);
        } finally {
            cursor.close();
        }
    }
    

    /** A raw-style query where you can pass any WHERE clause and arguments. */
    public List<LedimChoiceCardBean> queryDeep(String where, String... selectionArg) {
        Cursor cursor = db.rawQuery(getSelectDeep() + where, selectionArg);
        return loadDeepAllAndCloseCursor(cursor);
    }
 
}
